package com.lu.controller;

import com.lu.bean.AppException;
import com.lu.bean.ResBean;
import com.lu.constant.ErrorEnum;
import com.lu.dao.mapper.WgRiddleMapper;
import com.lu.service.SubmitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 文件上传/下载
 * @author luke
 * @className FileController
 * @date 2021/07/13 13:48
 */
@Slf4j
@RestController
@Api(tags = "文件上传/下载")
public class FileController {

    @Value("${config.imgPath}")
    private String imgPath;

    @Resource
    private WgRiddleMapper wgRiddleMapper;

    @Resource
    private SubmitService submitService;

    /**
     * 查看图片的接口
     * @author luke
     * @date 2021/7/13 14:03
     * @param fileName
     * @param response
     * @return void
     */
    @ApiOperation("图片下载")
    @RequestMapping(value = "download/img", method = {RequestMethod.POST, RequestMethod.GET})
    public void downloadImg(@ApiParam(value = "图片编号", required = true) @RequestParam String fileName,
                            HttpServletResponse response){
        String absolutePath = imgPath + fileName;
        File file = new File(absolutePath);
        if(!file.exists()) {
            log.error("file [{}] does not exist,download fail", absolutePath);
            return ;
        }

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            bos = new BufferedOutputStream(response.getOutputStream());

            byte[] b = new byte[1024];
            while(bis.read(b) != -1) {
                bos.write(b);
            }
            bos.flush();
        } catch (Exception e) {
            log.error("file download error,",e);
        } finally {
            try {
                if(bis != null) {
                    bis.close();
                }
                if(bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                log.error("file close error,",e);
            }
        }
    }

    /**
     * 提交画作前，先上传的图片，返回图片链接
     * @author luke
     * @date 2021/7/13 11:57
     * @param img
     * @return com.lu.bean.ResBean
     */
    @ApiOperation("图片上传")
    @RequestMapping(value = "/upload/uploadImg", method = RequestMethod.POST)
    public ResBean uploadImg(@ApiParam(value = "文件", required = true) @RequestParam MultipartFile img) {
        ResBean res = new ResBean();
        try {
            String path = submitService.uploadImg(img);
            res.setData(path);
        } catch (AppException e) {
            log.error("AppException occur,{},", e, e);
            res.setByAppException(e);
        } catch (Exception e) {
            log.error("Exception occur,",e);
            res.setResCode(ErrorEnum.FAIL.code);
            res.setResMsg("服务器异常");
        }
        return res;
    }

}
